package com.sphd.emanu.mvp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sphd.emanu.R;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.retrofitUtil.EmanuService;
import com.sphd.emanu.retrofitUtil.HttpApi;
import com.squareup.picasso.Picasso;
import com.wzutils.L;

import java.io.File;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.pedant.SweetAlert.SweetAlertDialog;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;


/**
 * @Author Hu
 * @Time 2017/4/7 -- 14:19.
 * @Email 87683202@qq.com
 */

public abstract class MVPBaseActivity<V extends BaseView, T extends BasePresenterImpl<V>> extends AppCompatActivity implements BaseView {
    public T mPresenter;
    public Context context;
    public SweetAlertDialog sweetAlertDialog;
    private SwipeRefreshLayout myRefreshLayout;
    public View emptyView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        mPresenter = getInstance(this, 1);
        mPresenter.attachView((V) this);
        emptyView=View.inflate(context,R.layout.layout_emptyview,null);
    }

    public void showProgressDialog() {
        if (sweetAlertDialog == null) {
            sweetAlertDialog = new SweetAlertDialog(context, SweetAlertDialog.PROGRESS_TYPE);
            sweetAlertDialog.setTitleText("加载中……");
        }
        sweetAlertDialog.show();
    }

    public void setImage(ImageView imageView, String url) {
        Picasso.with(context)
                .load(url)
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .into(imageView);
    }

    public void setImageNet(ImageView imageView, String url) {
        Picasso.with(context)
                .load(Constant.HTTPTITLE_Base + url)
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .into(imageView);
    }

    @Override
    public void dissProgressDialog() {
        if (myRefreshLayout != null) {
            if (myRefreshLayout.isRefreshing()) {
                myRefreshLayout.setRefreshing(false);
            }
        }

        if (sweetAlertDialog != null) {
            sweetAlertDialog.dismiss();
        }
    }

    public MultipartBody.Part prepareFilePart(String partName, String path) {
        File file = new File(path);
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("multipart/form-data"), file);
        return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
    }

    public void setGONE(View view) {
        view.setVisibility(View.GONE);
    }

    public void setVISIBLE(View view) {
        view.setVisibility(View.VISIBLE);
    }

    public void setINVISIBLE(View view) {
        view.setVisibility(View.INVISIBLE);
    }

    public void setVeiwEnabled(View view, boolean isCan) {
        view.setEnabled(isCan);
    }

    public void setRefresh(SwipeRefreshLayout myRefreshLayout) {
        this.myRefreshLayout = myRefreshLayout;
        myRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_light, android.R.color.holo_red_light,
                android.R.color.holo_orange_light, android.R.color.holo_green_light);
    }

    public void setRecy(RecyclerView recy) {

        recy.setHasFixedSize(true);
        recy.setLayoutManager(new LinearLayoutManager(context));
        recy.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }

    public void setGRecy(RecyclerView recy, int i) {

        recy.setHasFixedSize(true);

        recy.setLayoutManager(new GridLayoutManager(context, i));

    }

    public AlertDialog getDialogBuilder(View view) {
        return new AlertDialog.Builder(this).setView(view).create();
    }

    public AlertDialog getDialogBuilder(View view, int style) {
        return new AlertDialog.Builder(this, style).setView(view).create();
    }

    public AlertDialog getDialogBuilder(View view, String title, String PositiveButton, String NegativeButton) {
        return new AlertDialog.Builder(this).setView(view).setTitle(title).setPositiveButton(PositiveButton, null).
                setNegativeButton(NegativeButton, null).create();
    }

    public AlertDialog getDialogBuilder(View view, int style, String title, String PositiveButton, String NegativeButton) {
        return new AlertDialog.Builder(this, style).setView(view).setTitle(title).setPositiveButton(PositiveButton, null).
                setNegativeButton(NegativeButton, null).create();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }

    @Override
    public Context getContext() {
        return this;
    }


    public <T> T getInstance(Object o, int i) {
        try {
            return ((Class<T>) ((ParameterizedType) (o.getClass()
                    .getGenericSuperclass())).getActualTypeArguments()[i])
                    .newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
        return null;
    }


    public void setTitleBar(Toolbar toolbar, String title) {
        toolbar.setTitle(title.trim());
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    public void setTitleBarNoBack(Toolbar toolbar, String title) {
        toolbar.setTitle(title.trim());
        setSupportActionBar(toolbar);

    }

    public void toast(Object o) {
        if (o instanceof Integer) {
            Toast.makeText(context, (Integer) o, Toast.LENGTH_SHORT).show();
        } else if (o instanceof CharSequence) {
            Toast.makeText(context, (CharSequence) o, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(context, o + "", Toast.LENGTH_SHORT).show();
        }

    }

    public void startActivityMine(Intent intent, Bundle bundle) {
        intent.putExtras(bundle);
        startActivity(intent);
    }

    public void startActivityMine(Class<?> cls) {
        Intent intent = new Intent(context, cls);
        Bundle bundle = new Bundle();
        intent.putExtras(bundle);
        startActivity(intent);
    }

    public void changeFragment(int rid, Fragment targetFragment, String tag) {
        this.getSupportFragmentManager()
                .beginTransaction()
                .replace(rid, targetFragment, tag)
                .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                .commit();
    }

    public boolean isCanLook(List<Moudle> moudles, Integer menuId) {
        boolean flag = false;
        for (Moudle moudle : moudles) {
            if (moudle.getMoudleId().equals(menuId)) {
                flag = moudle.getPermissionInfo().isLookOver();
                break;
            }
        }
        return flag;
    }

    public boolean isCanDo(List<Moudle> moudles, Integer menuId) {
        boolean flag = false;
        for (Moudle moudle : moudles) {
            if (moudle.getMoudleId().equals(menuId)) {
                flag = moudle.getPermissionInfo().isOperation();
                break;

            }

        }
        return flag;
    }
}
